﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace USB_Backup
{
    public partial class Form1 : Form
    {

         /*
         * Sam Leach 2014
         * Utility designed for use in schools to prompt Students to automatically backup removable storage devices
         * "USB Backup"
         * 
         */

        string driveLetter = "";
        public Form1(string driveLtr)
        {
            driveLetter = driveLtr;//globaly define the destination drive letter, its kind of important
            InitializeComponent();
            var drives = DriveInfo.GetDrives().Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable);
            string driveList = "";
            foreach (DriveInfo d in drives)
            {
                driveList += d.Name + "\n";
            }
            lblDrives.Text = driveList;

            //need to get all those drives again, and show what they are on the form

        }

        private void btnNo_Click(object sender, EventArgs e)
        {
            //well that was rude, I wrote all this for nothing!
            Environment.Exit(0);
        }

        private void btnYes_Click(object sender, EventArgs e)
        {
            // Arry of extensions we want to backup
            string[] extensions = new String[] { ".docx", ".doc", ".xls", ".xlsx", ".ppt", ".pptx" };
            // Path to backup to
            string destinationPath = driveLetter+":\\USB Backup\\";

            //check if the folder exists and if not create it
            bool isExists = System.IO.Directory.Exists(destinationPath);
            if (!isExists)
                System.IO.Directory.CreateDirectory(destinationPath);


            var drives = DriveInfo.GetDrives().Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable);
            /*
             * this bit is actually quite ugly
             * Loop through all removable drives
             * -> loop through each defined extension type
             * - -> copy from the root directory
             * - - -> then loop through every sub-directory
             * 
             * each directory has to be checked individually, mainly because of "System Volume Information" which will cause access denied errors and fail
             */
            
            foreach (DriveInfo d in drives)
            {
                foreach (string extension in extensions)
                {
                    try
                    {
                        //copy files at root directory
                        copyFiles(d.Name, destinationPath, extension);

                        foreach (string subDir in Directory.GetDirectories(d.Name))
                        {
                            //work through subdirectories and copy their files too
                            try
                            {
                                copyFiles(subDir, destinationPath, extension);
                            }
                            catch
                            {
                                // swallow, log, whatever
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show("Backup Failed");
                        Environment.Exit(100);
                    }
                }
            }
            MessageBox.Show("Backup Finished!");//wahoo
            Environment.Exit(0);//we need to exit otherwise it will just keeping going, forever!
        }

        public void copyFiles(string filePath, string destination, string extension)
        {
            //little function to do the copies, takes the source path, destination path and file extension to copy
            string[] files = Directory.GetFiles(filePath, "*" + extension, SearchOption.TopDirectoryOnly);
            string sourceFile = "";
            foreach (string sourcePath in files)
            {
                string[] parts = sourcePath.Split('\\');
                foreach (string part in parts)
                {
                    sourceFile = part;
                }


                try
                {
                    System.IO.File.Copy(sourcePath, destination + sourceFile, true);
                }
                catch (SystemException er)
                {
                    MessageBox.Show("Backup Failed");
                }
            }
        }
    }
}
